//  DTLZ5.java
//
//  Author:
//       Antonio J. Nebro <antonio@lcc.uma.es>
//       Juan J. Durillo <durillo@lcc.uma.es>
//
//  Copyright (c) 2011 Antonio J. Nebro, Juan J. Durillo
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.
// 
//  You should have received a copy of the GNU Lesser General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.

package jmetal.problems.DTLZ;

import hidra.experiments.Paramenters;
import hidra.jmetal.core.*;
import jmetal.encodings.solutionType.BinaryRealSolutionType;
import jmetal.encodings.solutionType.RealSolutionType;
import jmetal.util.JMException;
import jmetal.util.Configuration.*;

/**
 * Class representing problem DTLZ5
 */
public class DTLZ5vI extends DTLZ{

	

	 
	  private static final int K = 10;	
	  private int dimension_;
		  
		  
	  public DTLZ5vI(String solutionType, int dimension) throws ClassNotFoundException {
	    this(solutionType,Paramenters.NOBJ,dimension);
	  } // DTLZ1   
		    
		
	  public DTLZ5vI(String solutionType,Integer numberOfObjectives,int dimension) throws ClassNotFoundException {
		    this(solutionType, numberOfObjectives+K-1, numberOfObjectives,dimension);
	  } 
		
		
	
	
	
 /**
  * Creates a default DTLZ5 problem instance (12 variables and 3 objectives)
  * @param solutionType The solution type must "Real" or "BinaryReal". 
  */
  /*public DTLZ5(String solutionType) throws ClassNotFoundException {
    this(solutionType, 12,3);
  } // DTLZ5
  */
 /**
  * Creates a new DTLZ5 problem instance
  * @param numberOfVariables Number of variables
  * @param numberOfObjectives Number of objective functions
  * @param solutionType The solution type must "Real" or "BinaryReal". 
  */
  public DTLZ5vI(String  solutionType,
               Integer numberOfVariables,
               Integer numberOfObjectives, int dimension) throws ClassNotFoundException  {
    numberOfVariables_  = numberOfVariables.intValue();
    numberOfObjectives_ = numberOfObjectives.intValue();
    numberOfConstraints_= 0;
    problemName_        = "DTLZ5";
    dimension_ = dimension;
        
    lowerLimit_ = new double[numberOfVariables_];
    upperLimit_ = new double[numberOfVariables_];        
    for (int var = 0; var < numberOfVariables_; var++){
      lowerLimit_[var] = 0.0;
      upperLimit_[var] = 1.0;
    }    
     
    if (solutionType.compareTo("BinaryReal") == 0)
    	solutionType_ = new BinaryRealSolutionType(this) ;
    else if (solutionType.compareTo("Real") == 0)
    	solutionType_ = new RealSolutionType(this) ;
    else {
    	System.out.println("Error: solution type " + solutionType + " invalid") ;
    	System.exit(-1) ;
    }            
  } // DTLZ5
    
  /** 
  * Evaluates a solution 
  * @param solution The solution to evaluate
   * @throws JMException 
  */      
  public void evaluate(Solution solution) throws JMException {
    Variable[] gen  = solution.getDecisionVariables();
    
    double [] x = new double[numberOfVariables_];
    double [] f = new double[numberOfObjectives_];
    double [] theta = new double[numberOfObjectives_-1];
    double g = 0.0;
    int k = numberOfVariables_ - numberOfObjectives_ + 1;
                              
    for (int i = 0; i < numberOfVariables_; i++)
      x[i] = gen[i].getValue();
                
    for (int i = numberOfVariables_ - k; i < numberOfVariables_; i++)
      g += (x[i] - 0.5)*(x[i] - 0.5);        
        
    double t = java.lang.Math.PI  / (4.0 * (1.0 + g)); 
    
    
    for(int i=0; i<(dimension_-1);i++)
    	theta[i] = x[i] * java.lang.Math.PI / 2.0;  
    
    
    for (int i = (dimension_-1); i < (numberOfObjectives_-1); i++) 
      theta[i] = t * (1.0 + 2.0 * g * x[i]);			
        
    for (int i = 0; i < numberOfObjectives_; i++)
      f[i] = 1.0 + g;
        
    for (int i = 0; i < numberOfObjectives_; i++){
      for (int j = 0; j < numberOfObjectives_ - (i + 1); j++)            
        f[i] *= java.lang.Math.cos(theta[j]);                
        if (i != 0){
          int aux = numberOfObjectives_ - (i + 1);
          f[i] *= java.lang.Math.sin(theta[aux]);
        } // if
    } //for
        
    for (int i = 0; i < numberOfObjectives_; i++)
      solution.setObjective(i,f[i]);                
  
  
   /* boolean flag = false;
    for (int i = 0; i < numberOfObjectives_; i++){
    	if(Double.isInfinite(solution.getObjective(i))  || Double.isNaN(solution.getObjective(i)) ){
    		flag = true;
    		System.out.println();
    	}
    }*/
  
    
    
  } // evaluate
  
  
  
  
  public String toString(){
		return "DTLZ5";	
		
  }
  
  @Override
  public boolean isAnalytic() {
  	// TODO Auto-generated method stub
  	return false;
  }
  
  
  @Override
  public double[] getMaximumValue() {
  	
  	
  	return null;
  }

  @Override
  public double[] getMinimumValue() {
  	
  	
  	return null;
  }


  
}
